﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Collections;

namespace SedionFormDesigner.Designer
{
    internal class SedionDesignSite : ISite, IDictionaryService
    {
        private IComponent component;
        private SedionDesignerHost host;
        private String name;
        private Hashtable dictionary;

        internal SedionDesignSite(SedionDesignerHost host, String name)
        {
            this.host = host;
            this.component = null;
            this.name = name;
        }

        /// <summary>
        ///  设置组件的名称
        /// </summary>
        internal void SetComponent(IComponent component)
        {
            Debug.Assert(this.component == null, "Cannot set a component twice");
            this.component = component;

            if (this.name == null)
                this.name = host.GetNewComponentName(component.GetType());
        }

        internal void SetName(String newName)
        {
            name = newName;
        }

        #region ISite 成员

        /// <summary>
        ///  返回Site对象的组件
        /// </summary>
        public IComponent Component
        {
            get
            {
                Debug.Assert(component != null, "Need the component before we've established it.");
                return component;
            }
        }

        /// <summary>
        ///  获得组件容器(来自host)
        /// </summary>
        public IContainer Container
        {
            get { return host.Container; }
        }

        /// <summary>
        ///  显示是否处在设计模式
        /// </summary>
        public bool DesignMode
        {
            get { return true; }
        }

        /// <summary>
        ///  获得/设置组件名称
        /// </summary>
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                if (value == null)
                    throw new ArgumentException("Bad Name Value - cannot be null");

                if (value.Equals(name)) return;
                host.OnComponentRename(new ComponentRenameEventArgs(component, name, value));
                name = value;
            }
        }

        #endregion

        #region IServiceProvider 成员

        /// <summary>
        ///  调用host.GetService来获取服务
        /// </summary>
        public object GetService(Type serviceType)
        {
            if (serviceType == typeof(IDictionaryService))
                return this;
            IServiceProvider provider = (IServiceProvider)host;
            return provider.GetService(serviceType);
        }

        #endregion

        #region IDictionaryService 成员

        public object GetKey(object value)
        {
            if (dictionary != null)
            {
                foreach (DictionaryEntry enty in dictionary)
                {
                    if (Object.Equals(enty.Value, value))
                        return enty.Key;
                }
            }
            return null;
        }

        public object GetValue(object key)
        {
            if (dictionary != null)
                return dictionary[key];
            return null;
        }

        public void SetValue(object key, object value)
        {
            if (dictionary == null)
                dictionary = new Hashtable();
            dictionary[key] = value;
        }

        #endregion
    }
}
